package com.hang.controller;

import com.hang.common.Result;
import com.hang.exception.MyException;
import com.hang.model.dto.base.BaseModel;
import com.hang.model.dto.request.ItemPageDTO;
import com.hang.model.dto.request.ItemQueryDTO;
import com.hang.model.dto.request.ItemUserDTO;
import com.hang.model.dto.response.ItemApplyInfoVO;
import com.hang.model.dto.response.ItemQueryListVO;
import com.hang.model.dto.response.ItemUserQueryListVO;
import com.hang.model.vo.UserInfoVO;
import com.hang.service.IItemService;
import com.hang.service.IItemUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "项目用户管理模块")
@RequestMapping("item")
@Slf4j
public class ItemUserController {
    @Resource
    private IItemUserService itemUserService;

    @Resource
    private IItemService itemService;

    @ApiOperation(value = "添加项目成员")
    @PostMapping("addItemUser")
    public Result<Object> addItemUser(@RequestBody @Valid ItemUserDTO itemUserDTO){
        try {
            itemUserService.addUser(itemUserDTO);
            return Result.ok().message("添加成功");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }

    @ApiOperation(value = "批量移除项目成员")
    @PostMapping("deleteItemUser")
    public Result<Object> removeUser(@RequestBody @Valid ItemUserDTO itemUserDTO){
        try {
            itemUserService.deleteUser(itemUserDTO);
            return Result.ok().message("删除成功");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }

    @ApiOperation(value = "分页查看项目成员")
    @PostMapping("getUserListByItem")
    public Result<ItemUserQueryListVO> getUserListByItem(
            @RequestBody @Valid ItemPageDTO itemPageDTO
    ){
        ItemUserQueryListVO itemUserQueryListVO = itemUserService.getUserListByItemId(itemPageDTO);
        return Result.ok(itemUserQueryListVO);
    }

    @ApiOperation(value = "用户查看自己的项目空间")
    @PostMapping("getMyItemByCondition")
    public Result<ItemQueryListVO> getMyItemByCondition(
            @RequestBody @Valid ItemQueryDTO itemQueryDTO
    ){
        ItemQueryListVO itemQueryListVO = itemService.getMyItemsByCondition(itemQueryDTO);
        return Result.ok(itemQueryListVO);
    }

    @ApiOperation(value = "用户申请加入项目")
    @GetMapping("apply")
    public Result<Object> apply(
            @RequestParam("itemId") @ApiParam(value = "项目id",required = true) Long itemId,
            @RequestParam("userId") @ApiParam(value = "用户id",required = true) Long userId
    ){
        try {
            itemService.applyInItem(itemId,userId);
            return Result.ok().message("申请成功");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }

    @ApiOperation(value = "项目经理查看申请列表")
    @GetMapping("applyList")
    public Result<ItemApplyInfoVO> getItemApplyList(
            @RequestParam("itemId") @ApiParam(value = "项目id",required = true) Long itemId,
            @RequestParam("userId") @ApiParam(value = "项目经理id",required = true) Long userId
    ){
        ItemApplyInfoVO itemApplyInfoVOList = itemService.getItemApplyList(itemId,userId);
        return Result.ok(itemApplyInfoVOList);
    }

    @ApiOperation(value = "申请列表审批")
    @GetMapping("approval")
    public Result<Object> approvalApply(
            @RequestParam("itemId") @ApiParam(value = "项目id",required = true) Long itemId,
            @RequestParam("userId") @ApiParam(value = "用户id",required = true) Long userId,
            @RequestParam("status") @ApiParam(value = "审批状态: 0:不同意 1:同意",required = true) Integer status
    ){
        try {
            itemUserService.approval(itemId,userId,status);
            return status==1? Result.ok().message("已将用户添加进该项目"):Result.ok().message("已拒绝用户进入该项目");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }
}